In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import chart_studio.plotly as py
import plotly.graph_objs as go 
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
pd.options.display.max_rows = 20
np.set_printoptions(precision = 4, suppress = True)
In [13]:
df = pd.read_csv("data.csv", header = 1)

#Top 10 highest suicide rates
top10_suicide = df[['Country', 'Both sexes.1']].rename(columns={'Both sexes.1': 'Suicide rate'})
top10_suicide = top10_suicide.groupby('Country')[['Suicide rate']].mean()
top10_suicide = top10_suicide.nlargest(10, 'Suicide rate')
display(top10_suicide)
Suicide rate
Country
Russian Federation 41.46
Lithuania 39.74
Belarus 35.78
Kazakhstan 30.70
Ukraine 28.80
Guyana 26.58
Republic of Korea 26.18
Latvia 25.38
Hungary 24.92
Suriname 24.72
In [3]:
#Suicide rates by country
world_data = df.pivot(index = 'Country', columns = 'Year', values = 'Both sexes.1')

#2000 map
map_data_2000 = world_data.reset_index()[["Country", 2000]]
layout = dict(title='2000 Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=map_data_2000['Country'], locationmode='country names', z=map_data_2000[2000], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2005 map
map_data_2005 = world_data.reset_index()[["Country", 2005]]
layout = dict(title='2005 Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=map_data_2005['Country'], locationmode='country names', z=map_data_2005[2005], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2010 map
map_data_2010 = world_data.reset_index()[["Country", 2010]]
layout = dict(title='2010 Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=map_data_2010['Country'], locationmode='country names', z=map_data_2010[2010], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2015 map
map_data_2015 = world_data.reset_index()[["Country", 2015]]
layout = dict(title='2015 Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=map_data_2015['Country'], locationmode='country names', z=map_data_2015[2015], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2016 map
map_data_2016 = world_data.reset_index()[["Country", 2016]]
layout = dict(title='2016 Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=map_data_2016['Country'], locationmode='country names', z=map_data_2016[2016], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)
In [4]:
#Male suicide rates by country
male = df.pivot(index = 'Country', columns = 'Year', values = 'Male.1')

#2000 map
male_map_data_2000 = male.reset_index()[["Country", 2000]]
layout = dict(title='2000 Male Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=male_map_data_2000['Country'], locationmode='country names', z=male_map_data_2000[2000], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2005 map
male_map_data_2005 = male.reset_index()[["Country", 2005]]
layout = dict(title='2005 Male Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=male_map_data_2005['Country'], locationmode='country names', z=male_map_data_2005[2005], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2010 map
male_map_data_2010 = male.reset_index()[["Country", 2010]]
layout = dict(title='2010 Male Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=male_map_data_2010['Country'], locationmode='country names', z=male_map_data_2010[2010], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2015 map
male_map_data_2015 = male.reset_index()[["Country", 2015]]
layout = dict(title='2015 Male Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=male_map_data_2015['Country'], locationmode='country names', z=male_map_data_2015[2015], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2016 map
male_map_data_2016 = male.reset_index()[["Country", 2016]]
layout = dict(title='2016 Male Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=male_map_data_2016['Country'], locationmode='country names', z=male_map_data_2016[2016], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)
In [5]:
#Male suicide rates by country
female = df.pivot(index = 'Country', columns = 'Year', values = 'Female.1')

#2000 map
female_map_data_2000 = female.reset_index()[["Country", 2000]]
layout = dict(title='2000 Female Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=female_map_data_2000['Country'], locationmode='country names', z=female_map_data_2000[2000], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2005 map
female_map_data_2005 = female.reset_index()[["Country", 2005]]
layout = dict(title='2005 Female Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=female_map_data_2005['Country'], locationmode='country names', z=female_map_data_2005[2005], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2010 map
female_map_data_2010 = female.reset_index()[["Country", 2010]]
layout = dict(title='2010 Female Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=female_map_data_2010['Country'], locationmode='country names', z=female_map_data_2010[2010], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2015 map
female_map_data_2015 = female.reset_index()[["Country", 2015]]
layout = dict(title='2015 Female Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=female_map_data_2015['Country'], locationmode='country names', z=female_map_data_2015[2015], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

#2016 map
female_map_data_2016 = female.reset_index()[["Country", 2016]]
layout = dict(title='2016 Female Suicide Rates', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=female_map_data_2016['Country'], locationmode='country names', z=female_map_data_2016[2016], colorscale='matter', colorbar={'title': 'Suicide Rates'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)
In [6]:
#Region suicide rates
df2 = pd.read_csv("data2.csv", header = 2)
region_suicide = df2[["WHO region", "2000.3", "2005.3", "2010.3", "2015.3", "2016.3"]]
region_suicide = region_suicide.rename(columns={"2000.3": "2000", "2005.3": "2005", "2010.3": "2010", "2015.3": "2015", "2016.3": "2016"})
region_suicide.plot(kind='barh', title='Suicide rates (per 100 000 population) under overall population by region', x='WHO region', xlabel='', grid=True).set_xlim([0, 40])

#male region suicide rates
male_region_suicide = df2[["WHO region", "2000.4", "2005.4", "2010.4", "2015.4", "2016.4"]]
male_region_suicide = male_region_suicide.rename(columns={"2000.4": "2000", "2005.4": "2005", "2010.4": "2010", "2015.4": "2015", "2016.4": "2016"})
male_region_suicide.plot(kind='barh', title='Suicide rates (per 100 000 population) under male population by region', x='WHO region', xlabel='', grid=True).set_xlim([0, 40])

#female region suicide rates
female_region_suicide = df2[["WHO region", "2000.5", "2005.5", "2010.5", "2015.5", "2016.5"]]
female_region_suicide = female_region_suicide.rename(columns={"2000.5": "2000", "2005.5": "2005", "2010.5": "2010", "2015.5": "2015", "2016.5": "2016"})
female_region_suicide.plot(kind='barh', title='Suicide rates (per 100 000 population) under female population by region', x='WHO region', xlabel='', grid=True).set_xlim([0, 40])
Out[6]:
(0.0, 40.0)
In [10]:
#Global suicide rates
global_suicide = region_suicide.loc[region_suicide["WHO region"] == 'Global'].rename(columns={'WHO region': 'Year'}).set_index('Year').transpose()

#Global male suicide rates
global_male_suicide = male_region_suicide.loc[male_region_suicide["WHO region"] == 'Global'].rename(columns={'WHO region': 'Year'}).set_index('Year').transpose()

#Global female suicide rates
global_female_suicide = female_region_suicide.loc[female_region_suicide["WHO region"] == 'Global'].rename(columns={'WHO region': 'Year'}).set_index('Year').transpose()

frames = [global_suicide, global_male_suicide, global_female_suicide]
suicide_and_gender = pd.concat(frames, axis=1)
suicide_and_gender.columns = ['Overall population', 'Male population', 'Female population']
suicide_and_gender.plot(kind='line', title='Global suicide rates (2000-2016)', xlabel='Year', ylabel='Global Suicide Rates', grid=True, legend=True)
Out[10]:
<AxesSubplot:title={'center':'Global suicide rates (2000-2016)'}, xlabel='Year', ylabel='Global Suicide Rates'>
In [8]:
#Happiness scores
happy2016 = pd.read_csv("2016.csv")

map_data_happiness = happy2016[["Country", "Happiness Score"]]
layout = dict(title='Happiness Score', geo=dict(showframe=False, projection={'type': 'natural earth'}))
data = go.Choropleth(locations=map_data_happiness['Country'], locationmode='country names', z=map_data_2000[2000], colorscale='matter', colorbar={'title': 'Happiness Scores'})
fig = go.Figure(data=data, layout=layout)
iplot(fig)

happiness_by_region16 = happy2016.groupby(["Region"])[["Happiness Score"]].mean()

global_happiness = happiness_by_region16["Happiness Score"].mean()
global_value = pd.DataFrame(data={'Happiness Score': [global_happiness]}, index=["Global"])

happiness_by_region16 = happiness_by_region16.rename(index={'Sub-Saharan Africa': 'Africa', 'Latin America and Caribbean': 'Americas', 'North America': 'Americas','Southern Asia': 'South-East Asia', 'Eastern Asia': 'South-East Asia','Southeastern Asia': 'South-East Asia', 'Central and Eastern Europe': 'Europe', 'Western Europe': 'Europe', 'Middle East and Northern Africa': 'Eastern Mediterranean', 'Australia and New Zealand': 'Western Pacific'})
happiness_by_region16 = happiness_by_region16.groupby(["Region"]).mean()
happiness_by_region16 = pd.concat([happiness_by_region16, global_value], axis=0)

#Suicide rates
suicides_by_region16 = region_suicide[['WHO region', '2016']].set_index('WHO region').rename(columns={'2016': 'Suicide Rate'})

frames = [happiness_by_region16, suicides_by_region16]
suicides_and_happiness = pd.concat(frames, axis=1).sort_values('Happiness Score', ascending=True)
In [9]:
happiness_by_region16 = suicides_and_happiness[['Happiness Score']]
suicides_by_region16 = suicides_and_happiness[['Suicide Rate']]

print('Suicide rate and happiness score by region')
display(suicides_and_happiness)

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plot(happiness_by_region16, 'g-')
ax2.plot(suicides_by_region16, 'b-')

ax1.set_ylabel('Happiness by region', color='g')
ax2.set_ylabel('Suicide rate by region', color='b', rotation=270, labelpad=15)
plt.title('Suicide rate against happiness score by region')
plt.draw()

ax1.tick_params(axis='x', labelrotation=45)
ax1.grid('on', which='major', axis='y')
ax2.grid('on', which='major', axis='y')
Suicide rate and happiness score by region
Happiness Score Suicide Rate
Africa 4.136421 7.4
South-East Asia 5.175447 13.2
Eastern Mediterranean 5.386053 3.9
Global 5.778442 10.6
Europe 6.028178 15.4
Americas 6.677875 9.8
Western Pacific 7.323500 10.2